package com.example.demo.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class CyUtils {

    //网络 查询 判断是否为成语
    public static boolean selectCYByNet(String str) {
        String get = sendRequest("https://www.chengyucidian.net/search/?type=1&keyword="+str, "GET");
        System.out.println("查询网络"+str);
        return get.contains("【"+str+"】的意思_成语词典");

    }


    //http请求
    public static String sendRequest(String urlParam,String requestType) {

        HttpURLConnection con = null;

        BufferedReader buffer = null;
        StringBuffer resultBuffer = null;

        try {
            URL url = new URL(urlParam);
            //得到连接对象
            con = (HttpURLConnection) url.openConnection();
            //设置请求类型
            con.setRequestMethod(requestType);
            //设置请求需要返回的数据类型和字符集类型
            con.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            //允许写出
            con.setDoOutput(true);
            //允许读入
            con.setDoInput(true);
            //不使用缓存
            con.setUseCaches(false);
            //得到响应码
            int responseCode = con.getResponseCode();

            if(responseCode == HttpURLConnection.HTTP_OK){
                //得到响应流
                InputStream inputStream = con.getInputStream();
                //将响应流转换成字符串
                resultBuffer = new StringBuffer();
                String line;
                buffer = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                while ((line = buffer.readLine()) != null) {
                    resultBuffer.append(line);
                }
                return resultBuffer.toString();
            }

        }catch(Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //返回第一个字的拼音
    public static List<String> getFirstPingYin(String str){
        return pingYinCh(str.charAt(0));
    }


    //返回最后一个字的拼音
    public static List<String> getLastPingYin(String str){
        return pingYinCh(str.charAt(str.length()-1));
    }



//    //中文转拼音
//    public String[] pingYin(String str){
//        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
//        //拼音小写
//        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//        //不带声调
//        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//        //要转换的中文，格式，转换之后的拼音的分隔符，遇到不能转换的是否保留   wo,shi,zhong,guo,ren,，hello
//        try {
//            return PinyinHelper.toHanYuPinyinString(str, format, ",", true).split(",");
//        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
//            badHanyuPinyinOutputFormatCombination.printStackTrace();
//        }
//        return null;
//    }


    //单个字 中文 转拼音 会有多音字
    public static List<String> pingYinCh(char ch){
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        //拼音小写
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //不带声调
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        //要转换的中文，格式，转换之后的拼音的分隔符，遇到不能转换的是否保留   wo,shi,zhong,guo,ren,，hello
        try {
            List<String> reList = new ArrayList<>();
            String[] list = PinyinHelper.toHanyuPinyinStringArray(ch, format);
//            Stream<String> distinct = Arrays.asList(list).stream().distinct();
            Stream<String> distinct = Arrays.stream(list).distinct();
            distinct.forEach(reList::add);
            return reList;
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return null;
    }

}
